package com.example.day05rk.view

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.lib_base.entity.ViewEntity
import com.example.lib_base.entity.ViewEntityItem
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ViewModel @Inject constructor(val apiService: ApiService):BaseViewModel() {

    fun getView(para:String,type:String):Flow<PagingData<ViewEntityItem>> {
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ViewDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }

    val searchViewData = MutableLiveData<ViewEntity>()

    fun getSearch(name:String) {
        httpRequest({apiService.searchName(name)},searchViewData::postValue)
    }
}